package com.gxa.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.common.base.RespResult;
import com.gxa.domain.dto.DepartmentPageDto;
import com.gxa.domain.dto.PageDto;
import com.gxa.domain.dto.SearchResultDto;
import com.gxa.domain.entity.Department;
import com.gxa.domain.entity.User;
import com.gxa.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 万事顺意
 * Created with IntelliJ IDEA.
 * User: zwj
 * Date: 2024/07/1710:50
 * Description:
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @GetMapping("/query")
    public RespResult query(Integer id) {
        Department byId = departmentService.getById(id);
        return RespResult.success(byId);
    }
    @PostMapping("/save")
    public RespResult save(Department department) {
        boolean save = departmentService.save(department);
        if (save) {
            return RespResult.success(save);
        }
        return RespResult.error("保存失败");
    }
    @GetMapping("/list")
    public RespResult list1(){
        List<Department> list= departmentService.list();
        return RespResult.success(list);
    }

    @PostMapping("/update")
    public RespResult update(Department department) {
        boolean b = departmentService.updateById(department);
        if (b) {
            return RespResult.success(b);
        }
        return RespResult.error("更新失败");
    }

    @PostMapping("/delete")
    public RespResult delete(Integer id) {
        boolean b = departmentService.removeById(id);
        if (b) {
            return RespResult.success(b);
        }
        return RespResult.error("删除失败");
    }

    @PostMapping("/search") // 注意这里我将 "seach" 改为了 "search"
    public RespResult search(DepartmentPageDto dto) {
        IPage<Department> page = new Page<>(dto.getPage(), dto.getLimit());
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        if (dto.getDeptName() != null && !dto.getDeptName().isEmpty()) {
            queryWrapper.like("dept_name", dto.getDeptName());
        }
        IPage<Department> result = departmentService.page(page, queryWrapper);
        //获取总记录数
        //获取分页数据
        SearchResultDto searchResultDto = new SearchResultDto();
        searchResultDto.setTotal(result.getTotal());
        searchResultDto.setRecords(result.getRecords());
        // 假设 RespResult 有一个静态方法来创建成功的响应，如 success(Object data)
        return RespResult.success(searchResultDto);
    }
}


